<template>
  <el-dialog
    v-el-drag-dialog
    title="转派"
    :visible.sync="visible"
    width="600px"
    @closed="reset"
  >
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="100px" style="margin: 0 50px;">
      <el-form-item label="责任机构:" prop="orgId">
        <el-tree-select
          v-model="temp.orgId"
          :clearable="false"
          class="tree-select"
          :popover-width="360"
          :select-params="{
            clearable: true,
            placeholder: '请选择',
            loading: $loadingPlugin.getTreeData
          }"
          :tree-params=" {
            'default-expand-all': true,
            'expand-on-click-node': false,
            'highlight-current': true,
            props:{
              value:'orgId',
              label: 'orgName',
              disabled: 'disabled',
            },
            data: treeData
          }"
          @node-click="selectTreeChange"
        />
      </el-form-item>
      <el-form-item label="处理人:" prop="userId">
        <el-select v-model="temp.userId" :loading="$loadingPlugin.getOwnersList" filterable style="width: 100%;" placeholder="请选择">
          <el-option
            v-for="item in ownersList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryReturnBottleOrderTransfer, queryOwnersList } from '@/api'
  import getTreeData from '@/utils/jsonTree'
  import { mapGetters } from 'vuex'

  export default {
    name: 'transfer-order',
    data() {
      return {
        visible: false,
        treeData: [],
        currentOrg: null,
        ownersList: [],
        preOwnerId: '',
        temp: {
          orgId: '',
          userId: '',
          preOwnerId: ''
        },
        rules: {
          orgId: [{ required: true, message: '请选择组织机构', trigger: 'change' }],
          userId: [{ required: true, message: '请选择处理人', trigger: 'blur' }]
        }
      }
    },
    computed: {
      ...mapGetters([
        'orgList', 'corpSettingData'
      ])
    },
    methods: {
      async open(temp) {
        this.preOwnerId = temp.userId
        await Promise.all([this.getTreeData(), this.getOwnersList(temp.orgId)])
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.currentOrg = this.orgList.find(item => item.orgId === temp.orgId)
            this.temp = Object.assign(this.temp, temp, {
              userId: ''
            })
          }
        })
      },
      // 获取责任机构数据
      async getTreeData() {
        const data = this.orgList.map(item => ({
          label: item.name,
          ...item
        }))
        this.treeData = getTreeData(data)
      },
      // 选择所属机构
      selectTreeChange(value) {
        this.$set(this.temp, 'userId', '')
        this.currentOrg = value
        this.getOwnersList()
      },
      // 查询处理人列表
      async getOwnersList(orgId) {
        const b = this.corpSettingData.sp1 >> 2 & 0x01
        const res = await queryOwnersList({
          orgId: orgId || this.temp.orgId,
          corpparm1: b
        })
        const copy = JSON.parse(JSON.stringify(res.data))
        if (this.temp.rstatus !== 6) {
          const index = res.data.findIndex(item => item.userId === this.preOwnerId)
          copy.splice(index, 1)
        }
        this.ownersList = copy
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const params = {
            backCode: this.temp.backCode,
            orgId: this.currentOrg.orgId,
            orgSNNo: this.currentOrg.orgSNNo,
            orgName: this.currentOrg.orgName,
            userId: this.temp.userId,
            preOwnerId: this.preOwnerId
          }
          this.submit(params)
        })
      },
      async submit(params) {
        await queryReturnBottleOrderTransfer(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '转派成功!',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.temp = {
          orgId: '',
          userId: '',
          preOwnerId: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style scoped>

</style>
